package com.itheima.service.pmsService.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.commons.PageBean;
import com.itheima.domain.db.pms.PmsProductCategory;
import com.itheima.mapper.pmsMapper.ProductCategoryMapper;
import com.itheima.service.pmsService.ProductCategoryService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    ProductCategoryMapper productCategoryMapper;

    @Override
    public List<PmsProductCategory> findAll() {
        List<PmsProductCategory> pmsProductCategories = productCategoryMapper.selectList(null);
        return pmsProductCategories;
    }

    @Override
    public void addProductCategory(PmsProductCategory pmsProductCategory) {
        productCategoryMapper.insert(pmsProductCategory);
    }

    @Override
    public void deleteProductCategory(Integer id) {
        productCategoryMapper.deleteById(id);
    }

    @Override
    public PageBean<PmsProductCategory> findPageProductCategory(Integer parentId, Integer pageNum, Integer pageSize) {

        IPage<PmsProductCategory> page = new Page<>(pageNum, pageSize);

        LambdaQueryWrapper<PmsProductCategory> query = new LambdaQueryWrapper<>();
        query.orderByAsc(PmsProductCategory::getSort);
        query.eq(PmsProductCategory::getParentId,parentId);

        productCategoryMapper.selectPage(page, query);

        PageBean<PmsProductCategory> pageBean = new PageBean<>(page);
        return pageBean;
    }

    @Override
    public void updateNavStatus(Integer navStatus, Integer[] ids) {

        for (Integer id : ids) {
            LambdaUpdateWrapper<PmsProductCategory> lqw = new LambdaUpdateWrapper<>();
            lqw.eq(PmsProductCategory::getId,id);
            lqw.set(PmsProductCategory::getNavStatus,navStatus);
            productCategoryMapper.update(null,lqw);
        }
    }

    @Override
    public void updateShowStatus(Integer[] ids, Integer showStatus) {
        for (Integer id : ids) {
            LambdaUpdateWrapper<PmsProductCategory> luw = new LambdaUpdateWrapper<>();
            luw.eq(PmsProductCategory::getId,id);
            luw.set(PmsProductCategory::getShowStatus,showStatus);
            productCategoryMapper.update(null,luw);
        }
    }

    @Override
    public void updateProductCategory(PmsProductCategory pmsProductCategory) {
        productCategoryMapper.updateById(pmsProductCategory);
    }

    @Override
    public PmsProductCategory selectById(Integer id) {
        PmsProductCategory pmsProductCategory = productCategoryMapper.selectById(id);
        return pmsProductCategory;
    }
}
